package level.editor.control.tile;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.FocusEvent;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import level.editor.control.IconConstants;
import level.editor.control.layer.LayerBlock;
import level.editor.control.layer.LayerControl;

public class TileLayerBlock extends LayerBlock
{
	public TileLayerBlock(TileLayer layer)
	{
		super();
		this.layer = layer;
		namePanel = null;
		textField = null;
		label = null;

		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		setMaximumSize(new Dimension(377, 30));
		setBackground(layer.isSelected() ? selected : unselected);
		setBorder(BorderFactory.createRaisedBevelBorder());
		setAlignmentX(Component.CENTER_ALIGNMENT);
		createDeleteButton();
		createNameField();
		createModButtons();
		createArrows();
		setFocusable(true);
		addMouseListener(this);
	}

	public void setSelected(boolean isSelected)
	{
		setBackground(isSelected ? selected : unselected);
		layer.setSelected(isSelected);
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (e.getActionCommand().equals("Delete"))
			System.out
					.println("Implement the delete layer function you retard");
		if (e.getActionCommand().equals("Field"))
		{
			changeName();
		}
		if (e.getActionCommand().equals("Vis"))
		{
			if (e.getSource() instanceof JButton)
			{
				JButton button = (JButton) e.getSource();
				button.setIcon((!layer.isVisible() ? new ImageIcon(
						IconConstants.ICON_DIR + "/eye_open.png")
						: new ImageIcon(IconConstants.ICON_DIR
								+ "/eye_close.png")));
				layer.setVisible(!layer.isVisible());
				layer.requestMinimap();
				layer.getParent().repaint();
				doNotChangeName();
			}
		}
		if (e.getActionCommand().equals("Lock"))
		{
			if (e.getSource() instanceof JButton)
			{
				JButton button = (JButton) e.getSource();
				button.setIcon((!layer.isLocked() ? new ImageIcon(
						IconConstants.ICON_DIR + "/lock_close.png")
						: new ImageIcon(IconConstants.ICON_DIR
								+ "/lock_open.png")));
				layer.setLock(!layer.isLocked());
				layer.requestMinimap();
				layer.getParent().repaint();
				doNotChangeName();
			}
		}
	}

	@Override
	public void mouseClicked(MouseEvent e)
	{
		if (e.getSource() == labelPanel)
		{
			CardLayout layout = (CardLayout) namePanel.getLayout();
			layout.show(namePanel, "Field");
			textField.setCaretPosition(textField.getText().length());
			textField.requestFocusInWindow();
			textField.selectAll();
		}
		if (e.getSource() == this)
		{
			setSelected();
			doNotChangeName();
		}
	}

	@Override
	public void focusLost(FocusEvent e)
	{
		CardLayout layout = (CardLayout) namePanel.getLayout();
		textField.setText(label.getText());
		layout.show(namePanel, "Label");
	}

	private void setSelected()
	{
		((LayerControl) getParent()).setSelected(this);
		if (getBackground() == unselected)
		{
			setSelected(true);
		}
		else if (getBackground() == selected)
			setSelected(false);
		main.repaint();
	}

	private void createDeleteButton()
	{
		JButton delete = new JButton(new ImageIcon(IconConstants.ICON_DIR
				+ "/delete.png"));

		delete.setActionCommand("Delete");

		initiateButton(delete);
		add(delete);
		add(Box.createRigidArea(new Dimension(15, 0)));
	}

	private void createNameField()
	{
		namePanel = new JPanel(new CardLayout());
		labelPanel = new JPanel();
		labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.LINE_AXIS));
		label = new JLabel(layer.getName());
		label.setBackground(new Color(248, 248, 248));
		labelPanel.addMouseListener(this);
		textField = new JTextField(layer.getName(), 30);
		textField.setBackground(Color.white);
		textField.addActionListener(this);
		textField.setActionCommand("Field");
		textField.addFocusListener(this);
		labelPanel.add(Box.createRigidArea(new Dimension(3, 0)));
		labelPanel.add(label);
		namePanel.add(labelPanel, "Label");
		namePanel.add(textField, "Field");
		namePanel.setAlignmentY(CENTER_ALIGNMENT);
		namePanel.setMinimumSize(new Dimension(120, 20));
		namePanel.setMaximumSize(new Dimension(120, 20));
		namePanel.setPreferredSize(new Dimension(120, 20));
		namePanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
		add(namePanel);
	}

	private void createModButtons()
	{
		JButton vis = new JButton((layer.isVisible() ? new ImageIcon(
				IconConstants.ICON_DIR + "/eye_open.png") : new ImageIcon(
				IconConstants.ICON_DIR + "/eye_close.png")));
		JButton lock = new JButton((layer.isLocked() ? new ImageIcon(
				IconConstants.ICON_DIR + "/lock_close.png") : new ImageIcon(
				IconConstants.ICON_DIR + "/lock_open.png")));

		vis.setActionCommand("Vis");
		lock.setActionCommand("Lock");

		initiateButton(vis);
		initiateButton(lock);

		add(Box.createRigidArea(new Dimension(35, 0)));
		add(vis);
		add(Box.createRigidArea(new Dimension(30, 0)));
		add(lock);
		add(Box.createRigidArea(new Dimension(30, 0)));
	}

	private void createArrows()
	{
		JButton up = new JButton(new ImageIcon(IconConstants.ICON_DIR
				+ "/up_arrow.png"));
		JButton down = new JButton(new ImageIcon(IconConstants.ICON_DIR
				+ "/down_arrow.png"));

		up.setActionCommand("Up");
		down.setActionCommand("Down");
		initiateButton(up);
		initiateButton(down);
		add(up);
		add(down);
	}

	private void initiateButton(JButton button)
	{
		button.setAlignmentY(CENTER_ALIGNMENT);
		button.setOpaque(false);
		button.setMaximumSize(new Dimension(23, 23));
		button.setPreferredSize(new Dimension(23, 23));
		button.setMinimumSize(new Dimension(23, 23));
		button.setContentAreaFilled(false);
		button.setFocusPainted(false);
		button.addActionListener(this);
	}

	private void changeName()
	{
		CardLayout layout = (CardLayout) namePanel.getLayout();
		// Ask LayerControl for validity here

		layer.setName(textField.getText());
		label.setText(layer.getName());
		layout.show(namePanel, "Label");
		main.repaint();
	}

	private void doNotChangeName()
	{
		CardLayout layout = (CardLayout) namePanel.getLayout();

		layout.show(namePanel, "Label");
		textField.setText(label.getText());
		main.repaint();
	}

	private JPanel namePanel;
	private JTextField textField;
	private JLabel label;
	private JPanel labelPanel;
	private TileLayer layer;
	private Color selected = new Color(20, 124, 26);
	Color unselected = new Color(248, 248, 248);
}
